home *** CD-ROM | disk | FTP | other *** search
/ QuickTime 2.0 Developer Kit / QuickTime 2.0 Developer Kit.iso / mac / MAC / Programming Stuff / Sample Code / Sequence Grabber / Example Video Panel / ExampleVideoPanel.c < prev    next >
Encoding:
Text File  |  1993-06-23  |  16.5 KB  |  662 lines  |  [TEXT/KAHL]

  1. /*
  2.     File:        ExampleVideoPanel.c
  3.  
  4.     Contains:    Example video panel component routines.
  5.  
  6.     Written by:    Gary Woodcock
  7.  
  8.     Copyright:    © 1992 by Apple Computer, Inc., all rights reserved.
  9.  
  10.     Change History (most recent first):
  11.  
  12. */
  13.  
  14. //-----------------------------------------------------------------------
  15. // includes
  16.  
  17. #include "ExampleVideoPanelPrivate.h"
  18. #include <QuickTimeComponents.h>
  19. #include <Errors.h>
  20. #include <Resources.h>
  21. #include <SysEqu.h>
  22.  
  23. //-----------------------------------------------------------------------
  24.  
  25. #ifdef DEBUG_IT
  26.  
  27. // Use this declaration when we're running linked (for debugging)
  28. pascal ComponentResult
  29. ExampleVideoPanelDispatcher (ComponentParameters *params, Handle storage)
  30.                                              
  31. #else
  32.  
  33. // Use this declaration when we're a standalone component
  34. pascal ComponentResult
  35. main (ComponentParameters *params, Handle storage)
  36.  
  37. #endif DEBUG_IT
  38.  
  39. {
  40.     // This routine is the main dispatcher for the component
  41.     
  42.     ComponentResult        result = noErr;
  43.     ComponentFunction    exampleFunction = nil;
  44.     
  45.     // Did we get a Component Manager request code (< 0)?
  46.     if (params->what < 0)
  47.     {
  48.         switch (params->what)
  49.         {
  50.             case kComponentOpenSelect:            // Open request
  51.             {
  52.                 exampleFunction = _ExampleVideoPanelOpen;
  53.                 break;
  54.             }
  55.             case kComponentCloseSelect:            // Close request
  56.             {
  57.                 exampleFunction = _ExampleVideoPanelClose;
  58.                 break;
  59.             }
  60.             case kComponentCanDoSelect:            // Can Do request
  61.             {
  62.                 result = CallComponentFunction (params, 
  63.                     (ComponentFunction) _ExampleVideoPanelCanDo);
  64.                 break;
  65.             }
  66.             case kComponentVersionSelect:        // Version request
  67.             {
  68.                 result = CallComponentFunction (params,
  69.                     (ComponentFunction) _ExampleVideoPanelVersion);
  70.                 break;
  71.             }
  72.             case kComponentTargetSelect:        // Target request not supported
  73.             case kComponentRegisterSelect:        // Register request not supported
  74.             default:                            // Unknown request
  75.             {
  76.                 result = badComponentSelector;
  77.                 break;
  78.             }
  79.         }
  80.     }
  81.     else    // Was it one of our request codes?
  82.     {
  83.         switch (params->what)
  84.         {
  85.             case kSGCPanelGetDitlSelect:        // SGPanelGetDitl request
  86.             {
  87.                 exampleFunction = _ExampleVideoPanelGetDitl;
  88.                 break;
  89.             }
  90.             case kSGCPanelGetTitleSelect:        // SGPanelGetTitle request
  91.             {
  92.                 exampleFunction = _ExampleVideoPanelGetTitle;
  93.                 break;
  94.             }
  95.             case kSGCPanelCanRunSelect:            // SGPanelCanRun request
  96.             {
  97.                 exampleFunction = _ExampleVideoPanelCanRun;
  98.                 break;
  99.             }
  100.             case kSGCPanelInstallSelect:        // SGPanelInstall request
  101.             {
  102.                 exampleFunction = _ExampleVideoPanelInstall;
  103.                 break;
  104.             }
  105.             case kSGCPanelEventSelect:            // SGPanelEvent request
  106.             {
  107.                 exampleFunction = _ExampleVideoPanelEvent;
  108.                 break;
  109.             }
  110.             case kSGCPanelItemSelect:            // SGPanelItem request
  111.             {
  112.                 exampleFunction = _ExampleVideoPanelItem;
  113.                 break;
  114.             }
  115.             case kSGCPanelRemoveSelect:            // SGPanelRemove request
  116.             {
  117.                 exampleFunction = _ExampleVideoPanelRemove;
  118.                 break;
  119.             }
  120.             case kSGCPanelSetGrabberSelect:        // SGPanelSetGrabber request
  121.             {
  122.                 exampleFunction = _ExampleVideoPanelSetGrabber;
  123.                 break;
  124.             }
  125.             case kSGCPanelSetResFileSelect:        // SGPanelSetResFile request
  126.             {
  127.                 exampleFunction = _ExampleVideoPanelSetResFile;
  128.                 break;
  129.             }
  130.             case kSGCPanelGetSettingsSelect:    // SGPanelGetSettings request
  131.             {
  132.                 exampleFunction = _ExampleVideoPanelGetSettings;
  133.                 break;
  134.             }
  135.             case kSGCPanelSetSettingsSelect:    // SGPanelSetSettings request
  136.             {
  137.                 exampleFunction = _ExampleVideoPanelSetSettings;
  138.                 break;
  139.             }
  140.             default:                            // Unknown request
  141.             {
  142.                 result = badComponentSelector;
  143.                 break;
  144.             }
  145.         }
  146.     }
  147.     if (exampleFunction != nil)
  148.     {
  149.         result = CallComponentFunctionWithStorage (storage, params, exampleFunction);
  150.     }
  151.     return (result);
  152. }
  153.                                              
  154. //-----------------------------------------------------------------------
  155.  
  156. pascal ComponentResult
  157. _ExampleVideoPanelOpen (Handle storage, ComponentInstance self)
  158. {
  159.     #pragma    unused (storage)
  160.     
  161.     PanelGlobalsHdl    globals;
  162.     ComponentResult    result = noErr;
  163.     
  164.     #ifdef THINK_C
  165.         SetComponentInstanceA5 (self, *(long *) CurrentA5);
  166.     #endif THINK_C
  167.     
  168.     // Can we open another instance?
  169.     if (CountComponentInstances ((Component) self) <= kMaxExampleVideoPanelInstances)
  170.     {
  171.         // Did we get our storage?
  172.         globals = (PanelGlobalsHdl) NewHandleClear (sizeof (PanelGlobals));
  173.         if (globals != nil)
  174.         {
  175.             // Keep a reference to self
  176.             (**globals).self = (Component) self;
  177.                         
  178.             // Set storage ref
  179.             SetComponentInstanceStorage (self, (Handle) globals);
  180.         }
  181.         else    // NewHandleClear failed
  182.         {
  183.             result = MemError();
  184.         }
  185.     }
  186.     else    // No more instances can be opened
  187.     {
  188.         result = kGenericError;
  189.     }
  190.     return (result);
  191. }
  192.  
  193. //-----------------------------------------------------------------------
  194.  
  195. pascal ComponentResult
  196. _ExampleVideoPanelClose (Handle storage, ComponentInstance self)
  197. {
  198.     PanelGlobalsHdl    globals = (PanelGlobalsHdl) storage;
  199.     ComponentResult    result = noErr;
  200.     
  201.     // Do we have any clean up to do?
  202.     if (globals != nil)
  203.     {
  204.         // Dispose globals
  205.         DisposHandle ((Handle) globals);
  206.     }
  207.     return (result);
  208. }
  209.  
  210. //-----------------------------------------------------------------------
  211.  
  212. pascal ComponentResult
  213. _ExampleVideoPanelCanDo (short selector)
  214. {
  215.     // Case on the request code
  216.     switch (selector)
  217.     {
  218.         // Component Manager request codes
  219.         case kComponentOpenSelect:
  220.         case kComponentCloseSelect:
  221.         case kComponentCanDoSelect:
  222.         case kComponentVersionSelect:
  223.         
  224.         // Sequence grabber panel component request codes
  225.         case kSGCPanelGetDitlSelect:    
  226.         case kSGCPanelGetTitleSelect:    
  227.         case kSGCPanelCanRunSelect:        
  228.         case kSGCPanelInstallSelect:    
  229.         case kSGCPanelEventSelect:        
  230.         case kSGCPanelItemSelect:        
  231.         case kSGCPanelRemoveSelect:        
  232.         case kSGCPanelSetGrabberSelect:
  233.         case kSGCPanelSetResFileSelect:
  234.         case kSGCPanelGetSettingsSelect:
  235.         case kSGCPanelSetSettingsSelect:
  236.         {
  237.             return (true);
  238.         }
  239.         
  240.         // Unsupported or unknown request codes
  241.         case kComponentRegisterSelect:    // Register request not supported
  242.         case kComponentTargetSelect:    // Target request not supported
  243.         default:                        // Not a request code we recognize
  244.         {
  245.             return (false); 
  246.         }
  247.     }
  248. }
  249.  
  250. //-----------------------------------------------------------------------
  251.  
  252. pascal ComponentResult
  253. _ExampleVideoPanelVersion (void)
  254. {
  255.     // Return the version info
  256.     return (exampleVideoPanelInterfaceRevision);
  257. }
  258.  
  259. //-----------------------------------------------------------------------
  260.  
  261. pascal ComponentResult
  262. _ExampleVideoPanelGetDitl (Handle storage, Handle *ditl)
  263. {
  264.     // This routine gets our ditl and hands it back to the sequence grabber
  265.     
  266.     #pragma unused (storage)
  267.  
  268.     ComponentResult    result = noErr;
  269.     Handle            panelDITL = GetResource ('DITL', kExampleVideoPanelDITLID);
  270.  
  271.     // Did we get the DITL resource okay?
  272.     result = ResError();    
  273.     if (panelDITL != nil)
  274.     {
  275.         // Detach it
  276.         DetachResource (panelDITL);
  277.     }
  278.     *ditl = panelDITL;
  279.     
  280.     return (result);
  281. }
  282.  
  283. //-----------------------------------------------------------------------
  284.  
  285. pascal ComponentResult
  286. _ExampleVideoPanelGetTitle (Handle storage, Str255 title)
  287. {
  288.     #pragma unused (storage)
  289.     #pragma unused (title)
  290.     
  291.     // Nothing to do here right now
  292.     return (paramErr);
  293. }
  294.  
  295. //-----------------------------------------------------------------------
  296.  
  297. pascal ComponentResult
  298. _ExampleVideoPanelCanRun (Handle storage, SGChannel c)
  299. {
  300.     #pragma unused (storage)
  301.     #pragma unused (c)
  302.  
  303.     // Our 'thng' resource has the channelFlagHasDependency flag set, 
  304.     // which means that this routine gets called to find out if
  305.     // it can run in the current environment.  This usually means
  306.     // finding out if the panel is compatible with the currently
  307.     // selected digitizer.  This is handy for adding manufacturer
  308.     // specific panels to the settings dialog.  We don't do anything
  309.     // here except SysBeep just to show we actually get called.
  310.     SysBeep(5);
  311.     
  312.     return (noErr);
  313. }
  314.  
  315. //-----------------------------------------------------------------------
  316.  
  317. pascal ComponentResult
  318. _ExampleVideoPanelInstall (Handle storage, SGChannel c, DialogPtr d,
  319.     short itemOffset)
  320. {
  321.     // Do our setup in this routine
  322.     
  323.     #pragma unused (d)
  324.     #pragma unused (itemOffset)
  325.     
  326.     PanelGlobalsHdl    globals = (PanelGlobalsHdl) storage;
  327.     ComponentResult    result = noErr;
  328.  
  329.     // Get our gray pattern
  330.     *(long *)((**globals).grayPat) = *(long *)((**globals).grayPat + 4) = 0x55AA55AA;
  331.     
  332.     // Get current black level
  333.     result = VDGetBlackLevelValue (SGGetVideoDigitizerComponent (c), &((**globals).savedBlackLevel));
  334.     if (result != noErr)
  335.     {
  336.         // Can't get the black level, so we can't do zero black level
  337.         HiliteControl ((ControlHandle) GetItemHandle (d, itemOffset + kZeroBlackButton), 255);
  338.     }
  339.  
  340.     return (noErr);
  341. }
  342.  
  343. //-----------------------------------------------------------------------
  344.  
  345. pascal ComponentResult
  346. _ExampleVideoPanelEvent (Handle storage, SGChannel c, DialogPtr d,
  347.     short itemOffset, EventRecord *theEvent, short *itemHit, Boolean *handled)
  348. {
  349.     // This routine is quite similar to a normal event filter proc.
  350.     
  351.     PanelGlobalsHdl    globals = (PanelGlobalsHdl) storage;
  352.     ComponentResult    result = noErr;
  353.     GrafPtr            savedPort;
  354.     PenState        savedPen;
  355.     
  356.     // Set up the port stuff
  357.     GetPort (&savedPort);
  358.     SetPort (d);
  359.     GetPenState (&savedPen);
  360.                         
  361.     // Assume we don't handle it
  362.     *handled = false;
  363.     
  364.     // Look for a key hit
  365.     if ((theEvent->what == keyDown) || (theEvent->what == autoKey))
  366.     {
  367.         char    theChar = theEvent->message & charCodeMask;
  368.         char    theKeyCode = ((theEvent->message & keyCodeMask) >> 8);
  369.         Boolean    cmdKeyDown = ((theEvent->modifiers & cmdKey) != 0) ? true : false;
  370.         
  371.         if (cmdKeyDown && (theKeyCode == kDKey))
  372.         {
  373.             unsigned short    maxBlackLevel = 0;
  374.             
  375.             // Fake a "Do it" button hit
  376.             FakeDialogButtonHit (d, itemOffset + kZeroBlackButton);
  377.             result = VDSetBlackLevelValue (SGGetVideoDigitizerComponent (c), &maxBlackLevel);
  378.             *itemHit = itemOffset + kZeroBlackButton;
  379.             *handled = true;
  380.         }
  381.         else if (cmdKeyDown && (theKeyCode == kRKey))
  382.         {
  383.             // Fake a "Reset" button hit
  384.             FakeDialogButtonHit (d, itemOffset + kResetButton);
  385.             result = VDSetBlackLevelValue (SGGetVideoDigitizerComponent (c), &((**globals).savedBlackLevel));
  386.             *itemHit = itemOffset + kResetButton;
  387.             *handled = true;
  388.         }
  389.     }
  390.     else if (theEvent->what == updateEvt)
  391.     {
  392.         Rect    r;
  393.         
  394.         // Draw the separator line whenever we get an update cuz I'm lazy
  395.         GetItemBox (d, itemOffset + kSeparator, &r);
  396.         PenPat ((**globals).grayPat);
  397.         FrameRect (&r);
  398.     }
  399.     
  400.     // Restore stuff
  401.     SetPenState (&savedPen);
  402.     SetPort (savedPort);
  403.     
  404.     return (result);
  405. }
  406.  
  407. //-----------------------------------------------------------------------
  408.  
  409. pascal ComponentResult
  410. _ExampleVideoPanelItem (Handle storage, SGChannel c, DialogPtr d,
  411.     short itemOffset, short itemNum)
  412. {
  413.     #pragma unused (d)
  414.     
  415.     PanelGlobalsHdl    globals = (PanelGlobalsHdl) storage;
  416.     ComponentResult    result = noErr;
  417.     short            theItem = itemNum - itemOffset;    // Remember to account for the item offset
  418.     
  419.     // What item got hit?
  420.     switch (theItem)
  421.     {
  422.         case kZeroBlackButton:
  423.         {
  424.             unsigned short    maxBlackLevel = 0;
  425.             
  426.             result = VDSetBlackLevelValue (SGGetVideoDigitizerComponent (c), &maxBlackLevel);
  427.             break;
  428.         }
  429.         case kResetButton:
  430.         {
  431.             result = VDSetBlackLevelValue (SGGetVideoDigitizerComponent (c), &((**globals).savedBlackLevel));
  432.             break;
  433.         }
  434.         default:
  435.         {
  436.             break;
  437.         }
  438.     }
  439.     return (result);
  440. }
  441.  
  442. //-----------------------------------------------------------------------
  443.  
  444. pascal ComponentResult
  445. _ExampleVideoPanelRemove (Handle storage, SGChannel c, DialogPtr d,
  446.     short itemOffset)
  447. {
  448.     #pragma unused (storage)
  449.     #pragma unused (c)
  450.     #pragma unused (d)
  451.     #pragma unused (itemOffset)
  452.     
  453.     // This is where you do your panel related cleanup.  Note that
  454.     // this is different from the cleanup you do in your close component
  455.     // routine.  Basically, this gets called when your ditl is getting
  456.     // removed from the dialog and a new one is being added (like when
  457.     // someone chooses a different panel from the panel popup menu).
  458.     // Examples of stuff you might do here include getting rid of any
  459.     // custom controls or popup menus, or saving panel settings.
  460.     
  461.     return (noErr);
  462. }
  463.  
  464. //-----------------------------------------------------------------------
  465.  
  466. pascal ComponentResult
  467. _ExampleVideoPanelSetGrabber (Handle storage, SeqGrabComponent sg)
  468. {
  469.     PanelGlobalsHdl    globals = (PanelGlobalsHdl) storage;
  470.     
  471.     // Save our grabber
  472.     (**globals).seqGrabber = sg;
  473.     
  474.     return (noErr);
  475. }
  476.  
  477. //-----------------------------------------------------------------------
  478.  
  479. pascal ComponentResult
  480. _ExampleVideoPanelSetResFile (Handle storage, short resRef)
  481. {
  482.     PanelGlobalsHdl    globals = (PanelGlobalsHdl) storage;
  483.     
  484.     // Since we don't have the channelFlagDontOpenResFile flag
  485.     // set in our 'thng' resource, the sequence grabber will 
  486.     // open our resource file for us.
  487.     
  488.     // Save our resfile ref
  489.     (**globals).resRefNum = resRef;
  490.     
  491.     return (noErr);
  492. }
  493.                                                          
  494. //-----------------------------------------------------------------------
  495.  
  496. pascal ComponentResult
  497. _ExampleVideoPanelGetSettings (Handle storage, SGChannel c, UserData *ud, long flags)
  498. {
  499.     #pragma unused (c)
  500.     #pragma unused (flags)
  501.     
  502.     ComponentResult    result = noErr;
  503.     UserData        userStuff = 0L;
  504.     
  505.     // You HAVE to give something valid back as user data when you
  506.     // return from this routine or your panel will fail to install.  
  507.     // What is being asked for basically amounts to a handle to some 
  508.     // state info specific to your panel.  We don't really have any 
  509.     // state info we care about, so we just hand back an empty user 
  510.     // data just to keep everyone happy.
  511.     
  512.     if ((result = NewUserData (&userStuff)) == noErr)
  513.     {
  514.         *ud = userStuff;
  515.     }
  516.     return (result);
  517. }
  518.                                                         
  519. //-----------------------------------------------------------------------
  520.  
  521. pascal ComponentResult
  522. _ExampleVideoPanelSetSettings (Handle storage, SGChannel c, UserData ud, long flags)
  523. {
  524.     #pragma unused (storage)
  525.     #pragma unused (c)
  526.     #pragma unused (ud)
  527.     #pragma unused (flags)
  528.     
  529.     // Here is where you would decode your user data structure and use
  530.     // the info to set the state of your panel items.  Again, we don't
  531.     // really have anything that has any meaningful state in our panel,
  532.     // so we ignore this.
  533.     
  534.     return (noErr);
  535. }
  536.                                                          
  537. //-----------------------------------------------------------------------
  538.  
  539. pascal ComponentResult
  540. _ExampleVideoPanelValidateInput (Handle storage, Boolean *ok)
  541. {
  542.     #pragma unused (storage)
  543.     
  544.     // This is where you do a sanity check on the user-definable
  545.     // items in your panel.  If there are any bad values, you
  546.     // should alert the user somehow, and return false for *ok.
  547.     // If everything's fine, return true.  We don't really have
  548.     // anything to check on in our example, so we return true
  549.     // all the time.
  550.     
  551.     *ok = true;
  552.     return (noErr);
  553. }
  554.  
  555. //-----------------------------------------------------------------------
  556.  
  557. static OSErr
  558. FakeButtonHit (ControlHandle theButton)
  559. {
  560.     OSErr    result = noErr;
  561.     
  562.     if (theButton != nil)
  563.     {
  564.         long    dummyTicks = 0L;
  565.         
  566.         HiliteControl (theButton, inButton);
  567.         Delay (8, &dummyTicks);
  568.         HiliteControl (theButton, 0);
  569.     }
  570.     else
  571.     {
  572.         result = nilHandleErr;
  573.     }
  574.     return (result);
  575. }
  576.  
  577. //-----------------------------------------------------------------------
  578.  
  579. static OSErr
  580. FakeDialogButtonHit (DialogPtr theDialog, short theButtonItem)
  581. {
  582.     OSErr    result = noErr;
  583.     
  584.     if (theDialog != nil)
  585.     {
  586.         result = FakeButtonHit ((ControlHandle) GetItemHandle (theDialog, theButtonItem));
  587.     }
  588.     else
  589.     {
  590.         result = nilHandleErr;
  591.     }
  592.     return (result);
  593. }
  594.  
  595. //-----------------------------------------------------------------------
  596.  
  597. static void
  598. SetUserItem (DialogPtr theDialog, short theItem, Handle userProc)
  599. {
  600.     Handle    item;
  601.     Rect    box;
  602.     short    itemType;
  603.     
  604.     GetDItem (theDialog, theItem, &itemType, &item, &box);
  605.     SetDItem (theDialog, theItem, itemType, userProc, &box);
  606. }
  607.  
  608. //-----------------------------------------------------------------------
  609.  
  610. static Handle
  611. GetItemHandle (DialogPtr theDialog, short theItem)
  612. {
  613.     Handle    item;
  614.     Rect    box;
  615.     short    itemType;
  616.     
  617.     GetDItem (theDialog, theItem, &itemType, &item, &box);
  618.     return (item);
  619. }
  620.  
  621. //-----------------------------------------------------------------------
  622.  
  623. static void
  624. GetItemBox (DialogPtr theDialog, short theItem, Rect *theRect)
  625. {
  626.     Handle    item;
  627.     short    itemType;
  628.     
  629.     GetDItem (theDialog, theItem, &itemType, &item, theRect);
  630. }
  631.  
  632. //-----------------------------------------------------------------------
  633.  
  634. #ifdef THINK_C
  635. #ifdef DEBUG_IT
  636. void
  637. RegisterExampleVideoPanel (void);
  638.  
  639. void
  640. RegisterExampleVideoPanel (void)
  641. {
  642.     ComponentDescription foo;
  643.     Handle h;
  644.  
  645.       foo.componentType = SeqGrabPanelType;
  646.       foo.componentSubType = 'vide';
  647.       foo.componentManufacturer = 'xmpl';
  648.       foo.componentFlags = channelFlagHasDependency | channelFlagDontOpenResFile;    // Indicate that we want a can run message
  649.       foo.componentFlagsMask = 0L;
  650.  
  651.     PtrToHand ("\pExample (linked)", &h, 17);
  652.     RegisterComponent (&foo, (void *)ExampleVideoPanelDispatcher, 0, h, 0, 0);
  653.     DisposHandle (h);
  654. }
  655. #endif DEBUG_IT
  656. #endif THINK_C
  657.  
  658. //-----------------------------------------------------------------------
  659.  
  660.  
  661.  
  662.